//
//  FirstSpecialViewController.m
//  BTRIP
//
//  Created by dllo on 15/11/22.
//  Copyright © 2015年 johnbk. All rights reserved.
//

#import "FirstSpecialViewController.h"
#import <AFHTTPSessionManager.h>
#import "SpecialTopView.h"
#import "FirstSpecialBigModel.h"
#import "FirstSpecialSecondModel.h"
#import "SpecialTopView.h"
#import "SpecialDetailImagetableViewCell.h"
#import "SpecialDetailTitleTableViewCell.h"
#import "SuitableHeight.h"
#import <SDWebImageManager.h>
#import <UIImageView+WebCache.h>
#import <MBProgressHUD.h>
#include "UMSocial.h"
@interface FirstSpecialViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) SpecialTopView *topView;
@property (nonatomic, retain) UIView *navView;
@property (nonatomic, retain) UIButton *popButton;
@property (nonatomic, retain) UIButton *selectButton;
@property (nonatomic, retain) UIButton *shareButton;
@property (nonatomic, retain) NSMutableArray *articleArray;
@property (nonatomic, retain) UIImageView *imgView;
@property (nonatomic, retain) MBProgressHUD *hud;
@property (nonatomic, retain) UIVisualEffectView *visual;
@end

@implementation FirstSpecialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    self.articleArray = [NSMutableArray array];
    
    [self createTableView];
    [self createNaviView];
    [self loadData];

    
    self.hud = [[MBProgressHUD alloc]init];
    [self.view addSubview:self.hud];
    [_hud release];
    [self.hud show:YES];
    
   }
#pragma mark 解析数据
- (void)loadData{
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    [session GET:[NSString stringWithFormat:@"http://chanyouji.com/api/articles/%@.json?page=1",self.ID] parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        NSDictionary *dic = responseObject;
        FirstSpecialBigModel *first = [[FirstSpecialBigModel alloc] init];
        [first setValuesForKeysWithDictionary:dic];
        self.topView.bigSpecial = first;
        
        [self.imgView sd_setImageWithURL:[NSURL URLWithString:first.image_url] placeholderImage:[UIImage imageNamed:@"background"]];
       
        NSArray *array = first.article_sections;
        for (NSDictionary *dic in array) {
            FirstSpecialSecondModel *second = [[FirstSpecialSecondModel alloc]init];
            [second setValuesForKeysWithDictionary:dic];
             [self.articleArray addObject:second];
            [second release];
        }
        
        [self.tableView reloadData];
        [self.hud hide:YES];
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        NSLog(@"  =  error: %@",error);
    }];
}

- (void)createTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, -20, self.view.frame.size.width, self.view.frame.size.height + 20) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.bounces = NO;
    [self.tableView release];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.topView = [[SpecialTopView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 250)];
    self.tableView.tableHeaderView = self.topView;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableView registerClass:[SpecialDetailImagetableViewCell class] forCellReuseIdentifier:@"imageCell"];
    [self.tableView registerClass:[SpecialDetailTitleTableViewCell class] forCellReuseIdentifier:@"titleCell"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    NSLog(@"%ld",self.articleArray.count);
    return self.articleArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FirstSpecialSecondModel *second = [self.articleArray objectAtIndex:indexPath.row];
    if (second.image_url.length != 0) {
        SpecialDetailImagetableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"imageCell"];
        cell.secondModel = second;
        return cell;
    }else{
        SpecialDetailTitleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"titleCell"];
        cell.secondModel = second;
        return cell;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
     FirstSpecialSecondModel *second = [self.articleArray objectAtIndex:indexPath.row];
    CGFloat descHeight = [SuitableHeight suitableHeight:second.textDescription andWidth:(KWIDTH - 40) andFont:14];
    if (second.image_url.length != 0){
         CGFloat imageHeight = second.image_height.integerValue * (KWIDTH - 40) / second.image_width.integerValue;
        return imageHeight + descHeight + 20;
    }else{
        if (second.title.length != 0) {
            
            return descHeight + 50;
            
        }else{
            return descHeight + 40;
        }
    }
}

#pragma mark 创建navView
- (void)createNaviView{
    self.navView = [[UIView alloc]initWithFrame:CGRectMake(0, -20, KWIDTH, 84)];
    [self.view addSubview:self.navView];
    [_navView release];
    self.navView.alpha = 0;
    
    self.imgView = [[UIImageView alloc]initWithFrame:self.navView.bounds];
    [self.navView addSubview:self.imgView];
    [self.imgView release];
    self.visual = [[UIVisualEffectView alloc]initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
    self.visual.frame = self.navView.bounds;
   
    [self.imgView addSubview:self.visual];
    [_visual release];
    
    
    
    self.popButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.popButton setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui-4"] forState:UIControlStateNormal];
    self.popButton.frame = CGRectMake(20 * SWIDTH, 35 * SWIDTH, 20 * SWIDTH , 20 * SWIDTH);
    [self.popButton addTarget:self action:@selector(popButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.popButton];
    
    //收藏按钮
//    self.selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    [self.selectButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xin"] forState:UIControlStateNormal];
//    self.selectButton.frame = CGRectMake(self.popButton.frame.origin.x + self.popButton.frame.size.width + 230 * SWIDTH, self.popButton.frame.origin.y, 20 * SWIDTH, 20 * SWIDTH);
//    [self.selectButton addTarget:self action:@selector(selectButtonAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:self.selectButton];
    
    //分享按钮
    self.shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.shareButton setBackgroundImage:[UIImage imageNamed:@"iconfont-share-4"] forState:UIControlStateNormal];
    self.shareButton.frame = CGRectMake(self.popButton.frame.size.width + self.popButton.frame.origin.x + 280, self.popButton.frame.origin.y, 20 * SWIDTH, 20 * SWIDTH);
    [self.shareButton addTarget:self action:@selector(shareButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.shareButton];
}
#pragma mark navView随着偏移量偏移事件
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y < 20) {
        [self.popButton setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui-4"] forState:UIControlStateNormal];
        [self.selectButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xin"] forState:UIControlStateNormal];
        [self.shareButton setBackgroundImage:[UIImage imageNamed:@"iconfont-share-3"] forState:UIControlStateNormal];
        self.navView.alpha = 0;
    }else if (scrollView.contentOffset.y > 20 && scrollView.contentOffset.y < self.topView.imageViewOfBackground.frame.size.height){
        self.navView.alpha = scrollView.contentOffset.y / self.topView.imageViewOfBackground.frame.size.height;
        self.visual.alpha = scrollView.contentOffset.y / self.topView.imageViewOfBackground.frame.size.height - 0.2;
        [self.popButton setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui-3"] forState:UIControlStateNormal];
        [self.shareButton setBackgroundImage:[UIImage imageNamed:@"iconfont-share-2"] forState:UIControlStateNormal];
        [self.selectButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xin-2"] forState:UIControlStateNormal];
    }else{
//        self.navView.backgroundColor = [UIColor whiteColor];
        self.navView.alpha = 1;
        [self.popButton setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui-3"] forState:UIControlStateNormal];
        [self.shareButton setBackgroundImage:[UIImage imageNamed:@"iconfont-share-2"] forState:UIControlStateNormal];
        [self.selectButton setBackgroundImage:[UIImage imageNamed:@"iconfont-xin-2"] forState:UIControlStateNormal];
    }
}

//popButton点击事件
- (void)popButtonAction:(UIButton *)button{
    [self.navigationController popViewControllerAnimated:YES];
}
//selectButton点击事件
- (void)selectButtonAction:(UIButton *)button{
    
}
//shareButton点击事件
- (void)shareButtonAction:(UIButton *)button{
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"56567db5e0f55a58fc00040d" shareText:[NSString stringWithFormat:@"http://chanyouji.com/api/articles/%@.json?page=1",self.ID] shareImage:nil shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,nil] delegate:NULL];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
